import { methodGetApi } from '@/api/publicApi'
import { getToken, setToken, removeToken } from '@/utils/publicUtils/auth'
import { resetRouter } from '@/router'
import jwtDecode from 'jwt-decode'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  roles: [],
  userInfo: {},
  isExpireToken: false,
  permissions: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_USERINFO: (state, info) => {
    state.userInfo = info
  },
  SET_EXPIRESTATE: (state, data) => {
    state.isExpireToken = data
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      methodGetApi('/login', { loginName: username.trim(), password: password })
        .then(response => {
          const { data } = response
          var userInfo = jwtDecode(data)
          commit('SET_TOKEN', data)
          // commit('SET_USERINFO', jwtDecode(data)) // 解析token获取用户信息
          commit('SET_USERINFO', userInfo)
          commit('SET_NAME', userInfo.username)
          commit('SET_AVATAR', userInfo.icon)
          setToken(data)
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  // get user info
  getInfo({ commit, state }) {
    var userInfo = jwtDecode(getToken())
    commit('SET_USERINFO', userInfo)
    commit('SET_NAME', userInfo.username)
    commit('SET_AVATAR', userInfo.icon)
  },
  // 获取用户信息
  GetInfo({ commit, state }, menuKey) {
    return new Promise((resolve, reject) => {
      const data = {
        token: state.token,
        menuKey: menuKey
      }
      methodGetApi('/getInfo', data).then(res => {
        const user = res.user
        const avatar = null
        // user.avatar == ""
        // 	// ? require("@/assets/images/profile.jpg")
        // 	: process.env.VUE_APP_BASE_API + user.avatar;
        if (res.roles && res.roles.length > 0) {
          // 验证返回的roles是否是一个非空数组
          commit('SET_ROLES', res.roles)
          commit('SET_PERMISSIONS', res.permissions)
        } else {
          commit('SET_ROLES', ['ROLE_DEFAULT'])
        }
        // commit('SET_USER', res.user)
        commit('SET_NAME', user.nickName)
        commit('SET_USERINFO', user)
        commit('SET_AVATAR', avatar)
        resolve(res)
      })
        .catch(error => {
          reject(error)
        })
    })
  },
  // user logout
  logout({ commit, state }) {
    commit('SET_TOKEN', '')
    commit('SET_ROLES', [])
    commit('SET_NAME', null)
    commit('SET_USERINFO', {})
    commit('SET_PERMISSIONS', [])
    removeToken()
    resetRouter()
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      commit('SET_NAME', null)
      commit('SET_USERINFO', {})
      commit('SET_EXPIRESTATE', false)
      removeToken()
      resolve()
    })
  },

  // 设置token是否过期过期
  setExpireToken({ commit }) {
    commit('SET_EXPIRESTATE', true)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
